package com.android.fragmentdemo.defineviews
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import com.android.fragmentdemo.R

class ArrowView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : View(context, attributeSet) {

    private var mWidth = 0
    private var mHeight = 0

    private var mStokeWidth = 0F

    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            //线头
            strokeCap = Paint.Cap.ROUND
            //拐角---(圆角)
            strokeJoin = Paint.Join.ROUND
            color = context.resources.getColor(R.color.color_FF1744)
            style = Paint.Style.STROKE
        }
    }

    private val mPath by lazy {
        Path()
    }

    private var mTouchCallback: ITouchCallback? = null

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        mWidth = when(MeasureSpec.getMode(widthMeasureSpec)) {
            MeasureSpec.EXACTLY -> {
                MeasureSpec.getSize(widthMeasureSpec)
            }
            else -> {
                context.resources.getDimension(R.dimen.dp_100).toInt()
            }
        } - paddingStart - paddingEnd

        mHeight = when(MeasureSpec.getMode(heightMeasureSpec)) {
            MeasureSpec.EXACTLY -> {
                MeasureSpec.getSize(heightMeasureSpec)
            }
            else -> {
                context.resources.getDimension(R.dimen.dp_100).toInt()
            }
        } - paddingTop - paddingBottom

        setMeasuredDimension(mWidth, mHeight)

        //设置线宽
        mStokeWidth = context.resources.getDimension(R.dimen.dp_3)
        mPaint0.strokeWidth = mStokeWidth
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        canvas?.apply {
            mPath.moveTo(mStokeWidth * 2, mStokeWidth)
            mPath.lineTo(mWidth.toFloat() - mStokeWidth * 2, mHeight / 2.0F)
            mPath.lineTo(mStokeWidth * 2, mHeight - mStokeWidth)
            drawPath(mPath, mPaint0)
        }
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        performClick()
        when (event?.action) {
            MotionEvent.ACTION_DOWN -> {
                mPaint0.color = resources.getColor(R.color.color_AE390A)
            }
            MotionEvent.ACTION_CANCEL -> {
                mPaint0.color = resources.getColor(R.color.color_FF1744)
            }
            MotionEvent.ACTION_UP -> {
                mPaint0.color = resources.getColor(R.color.color_FF1744)
                mTouchCallback?.onTouchUp()
            }
        }
        invalidate()
        return true
    }

    override fun performClick(): Boolean {
        return super.performClick()
    }

    fun setTouchCallback(mTouchCallback: ITouchCallback) {
        this.mTouchCallback = mTouchCallback
    }

    interface ITouchCallback {
        fun onTouchUp()
    }
}